package ink.xiaobaibai.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import ink.xiaobaibai.common.Common;
import ink.xiaobaibai.common.DeviceType;
import ink.xiaobaibai.common.SettingCommon;
import ink.xiaobaibai.entity.AdminUser;
import ink.xiaobaibai.entity.ProductDevice;
import ink.xiaobaibai.response.ResponseFormat;
import ink.xiaobaibai.service.IAdminUserService;
import ink.xiaobaibai.service.IProductDeviceService;
import ink.xiaobaibai.serviceOxy.IMqttService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @description: 基本信息类
 * @author: 小白白
 * @create: 2021-07-17
 **/

@RestController
@RequestMapping("/setting")
@Api(tags = "基本信息接口")
public class SettingController {

    @Autowired
    private IAdminUserService adminUserService;

    @Autowired
    private IMqttService mqttService;

    @Autowired
    private IProductDeviceService productDeviceService;

    @GetMapping("/jump")
    @ApiOperation("当前机器是否在线(一定要异步) || 机器码 + 对应业务机器['单次','月费','购买','酒店'] 不同场景四选一")
    public ResponseFormat jump(@RequestParam("deviceCode") String deviceCode,
                               @RequestParam("type") String type) {

        // todo 默认在线,后期需要关闭
        if (true) {
            return ResponseFormat.success();
        }

        // TODO 后期需要关闭特例
        if (deviceCode.startsWith("ok")) {
            return ResponseFormat.success();
        }

        boolean jump;
        if (type.equals(DeviceType.ONEPAY.type)) {
            jump = this.mqttService.checkOnePayDevice(deviceCode);
        } else if (type.equals(DeviceType.MON.type)) {
            jump = this.mqttService.checkDeviceByType(deviceCode, DeviceType.MON);
        } else if (type.equals(DeviceType.BUY.type)) {
            jump = this.mqttService.checkDeviceByType(deviceCode, DeviceType.BUY);
        } else if (type.equals(DeviceType.FREE.type)) {
            jump = this.mqttService.checkDeviceByType(deviceCode, DeviceType.FREE);
        } else {
            return ResponseFormat.fail("type错误");
        }
        return jump ? ResponseFormat.success() : ResponseFormat.fail("当前机器不在线");
    }

    @GetMapping("/is-supplier/{adminUserId}")
    @ApiOperation("当前管理员是否为供应商 || 是的话就不用进行交押金了")
    public ResponseFormat isSupplierAdminUser(@PathVariable("adminUserId") Integer adminUserId) {
        boolean flag = this.adminUserService.isSupplierAdminUser(adminUserId);
        return ResponseFormat.success(flag);
    }

    /**
     * 获取管理人员的姓名+电话
     */
    @GetMapping("/admin/{userId}")
    @ApiOperation("根据管理人员id获取其姓名和电话")
    public ResponseFormat getAdminUserInfo(@PathVariable("userId") Integer userId) {
        QueryWrapper<AdminUser> q1 = new QueryWrapper<>();
        q1.select("user_name", "user_phone");
        q1.eq("id", userId);
        AdminUser adminUser = this.adminUserService.getOne(q1);
        return ResponseFormat.success(adminUser);
    }

    /**
     * 获取默认业务员
     */
    @GetMapping("default/adminUser")
    @ApiOperation("获取默认业务员")
    public ResponseFormat defaultAdminUser() {
        QueryWrapper<AdminUser> q1 = new QueryWrapper<>();
        q1.select("user_name", "user_phone", "id");
        q1.eq("id", Common.DEFAULT_ADMIN_USER_ID);
        AdminUser adminUser = this.adminUserService.getOne(q1);
        return ResponseFormat.success(adminUser);
    }

    /**
     * 获取使用说明
     */
    @GetMapping("/useAgreement")
    @ApiOperation("获取使用说明")
    public ResponseFormat getInstructions() {
        return ResponseFormat.success(SettingCommon.getUseAgreement());
    }

    /**
     * 获取轮播图
     */
    @GetMapping("/carouselImage")
    @ApiOperation("获取轮播图")
    public ResponseFormat getCarouselImage() {
        return ResponseFormat.success(SettingCommon.getCarouselImage());
    }

    /**
     * 地图信息接口
     */
    @GetMapping("/mapInfo")
    @ApiOperation("地图信息接口 | 点亮多少个城市,点亮全国百分之多少")
    public ResponseFormat getMapInfo() {
        return ResponseFormat.success(SettingCommon.getMapInfo());
    }

    /**
     * 定点协议信息接口
     */
    @GetMapping("/agreement-onePay")
    @ApiOperation("定点协议信息接口")
    public ResponseFormat getOnePayAgreement() {
        return ResponseFormat.success(SettingCommon.getOnepayAgreement());
    }

    /**
     * 押金协议接口
     */
    @GetMapping("/agreement-deposit")
    @ApiOperation("押金协议接口")
    public ResponseFormat getDepositAgreement() {
        return ResponseFormat.success(SettingCommon.getDepositAgeement());
    }

    /**
     * 月费协议接口
     */
    @GetMapping("/agreement-monthly")
    @ApiOperation("月费协议接口")
    public ResponseFormat getMonthlyAgreement() {
        return ResponseFormat.success(SettingCommon.getMonthlyAggement());
    }

    /**
     * 购买协议接口
     */
    @GetMapping("/buy-monthly")
    @ApiOperation("购买协议接口")
    public ResponseFormat getBuyAgreement() {
        return ResponseFormat.success(SettingCommon.getBuyAggement());
    }

    /**
     * 主界面的入口的四个图文
     */
    @GetMapping("/main-imageInfo")
    @ApiOperation("主界面的四个图文")
    public ResponseFormat getMainImageInfo() {
        return ResponseFormat.success(SettingCommon.getMainImageInfo());
    }

    /**
     * 托管协议
     */
    @GetMapping("/agreement-escrow")
    @ApiOperation("托管协议")
    public ResponseFormat getEscrowAgreement() {
        return ResponseFormat.success(SettingCommon.getEscrowAgreement());
    }

    /**
     * 关于我们
     */
    @GetMapping("/about")
    @ApiOperation("关于我们")
    public ResponseFormat getAbout() {
        return ResponseFormat.success(SettingCommon.getEscrowAgreement());
    }

    /**
     * 获取机器产品接口
     */
    @GetMapping("/product-list")
    @ApiOperation("获取机器产品")
    public ResponseFormat getProductList(@RequestParam("pageNum") Integer pageNum,
                                         @RequestParam("pageSize") Integer pageSize) {
        Page<ProductDevice> page = this.productDeviceService.page(new Page<>(pageNum, pageSize));
        return ResponseFormat.success(page);
    }

}
